package com.testDemo.jdk8;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.*;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Auther: zouren
 * @Date: 2020/1/4 12:38
 * @Description: 大于：&gt;
 * <p>
 * 小于：&lt;
 * <p>
 * 大于等于：&gt;=
 * <p>
 * 小于等于：&lt;=
 */
public class StringTest {
    @Test
    public void containsIgnoreCase() {

        System.out.println(StringUtils.containsIgnoreCase("asdf-aa", "AS"));
        System.out.println(StringUtils.containsIgnoreCase("asdf-aa", "B"));
    }

    @Test
    public void joinWith() {

        System.out.println(StringUtils.joinWith("asdf-aa", "AS", "CC"));
        System.out.println(StringUtils.joinWith("asdf-aa", "AS"));
        System.out.println(StringUtils.appendIfMissing("asdf-aa", "B"));
    }

    @Test
    public void subStr() {
        String symbol = "abvcefghijk";
        System.out.println(StringUtils.substring(symbol, -6));
        System.out.println(StringUtils.substring(symbol, 0, 6));
    }

    @Test
    public void isEmty() {

        System.out.println(StringUtils.isNotEmpty(""));
        System.out.println(StringUtils.isNotEmpty(null));
        System.out.println(StringUtils.isNotEmpty(" "));
        System.out.println("==============");
        System.out.println(StringUtils.isBlank(""));
        System.out.println(StringUtils.isNoneBlank(null));
        System.out.println(StringUtils.isNoneBlank(" "));
    }

    @Test
    public void test() {
        String symbol = "EURUSD-STD";
        String sexCode = StringUtils.substring(symbol, 0, 6);
        System.out.println(sexCode);
        System.out.println(StringUtils.appendIfMissing(sexCode, ""));
        System.out.println(StringUtils.appendIfMissing("", sexCode));
    }

    @Test
    public void tet111() {
        System.out.println(StringUtils.startsWith("a123", "a"));
        System.out.println(StringUtils.startsWith("", "a"));
        System.out.println(StringUtils.startsWith(null, "a"));
    }

    @Test
    public void tet222() {
    }

    @Test
    public void test112121() {

        String str = "1.1%";
        List aa = new ArrayList();

        for (double i = 0; i <= 3; i = BigDecimal.valueOf(i).add(new BigDecimal("0.01")).doubleValue()) {
            if (i < 0.5) {
                aa.add("a" + i);
            } else if (i < 1) {
                aa.add(i);
            } else {
                aa.add(i + "%");
            }


        }

        String pattern = "^([0-2]{1}\\.[0]{1,2}\\%+$)|([0-1]{1}\\.\\d{1,2}\\%{0,1}$)";
        aa.forEach(row -> {
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(row.toString());
            System.out.println(row.toString() + "===" + m.matches());
        });


    }

    @Test
    public void aa() {
        String json = "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"aa\",\n" +
                "  \"data\": {\n" +
                "    \"list\": [\n" +
                "      {\n" +
                "        \"ocId\": \"权益ID \",\n" +
                "        \"orderNo\": \"权益ID \",\n" +
                "        \"ifRightItemFixed\": \"权益责任是否已固定 \",\n" +
                "        \"productInPack\": {\n" +
                "          \"productPack\": \"所属的服务产品\",\n" +
                "          \"servTimes\": \"服务次数\"\n" +
                "        }\n" +
                "      },\n" +
                "      {\n" +
                "        \"ocId\": \"1 \",\n" +
                "        \"orderNo\": \"1 \",\n" +
                "        \"ifRightItemFixed\": \"1\",\n" +
                "        \"productInPack\": {\n" +
                "          \"productPack\": \"1\",\n" +
                "          \"productPackaaa\": \"\",\n" +
                "          \"servTimes\": \"1\"\n" +
                "        }\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}\n";
        List<String> a = new ArrayList();
        a.add("a1");
        a.add("a2");
        a.add("a3");
        JSON parse = JSONUtil.parse(a);
//        removeKeys( parse);
        System.out.println(parse);

        JSONObject jsonObject = JSONUtil.parseObj(json);
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        jsonArray.add(a);

//        System.out.println(json);
//        removeKeys(jsonObject);
//        System.out.println(jsonObject);

        removeKeys(jsonArray);
        System.out.println(jsonArray);
    }

    public void removeKeys(JSON json) {
        Set<String> re = new HashSet<>();
        if (json != null) {
            if (json instanceof JSONArray) {
                JSONArray jsonArray = (JSONArray) json;
                if (CollectionUtil.isNotEmpty(jsonArray)) {
                    int len  =jsonArray.size();
                    Object row;
                    for (int i = 0; i < len; i++) {
                        row = jsonArray.get(i);
                        if (row instanceof JSONArray) {
                            removeKeys((JSONArray) row);
                        } else if (row instanceof JSONObject) {
                            removeKeys((JSONObject) row);
                        }else {
                            jsonArray.set(i,"*");
                        }
                    }
                }
            } else {
                JSONObject jsonObject = (JSONObject) json;
                jsonObject.keySet().forEach(row -> {
                    Object value = jsonObject.get(row);
                    if (value instanceof JSONArray) {
                        removeKeys((JSONArray) value);
                    } else if (value instanceof JSONObject) {
                        removeKeys((JSONObject) value);
                    } else if(StringUtils.isNotBlank(value.toString())){
                        jsonObject.set(row, "*");
                    }
                });
            }

        }
    }
    @Test
public void getMassageTest(){
        String aa = "fasfds4${aa}是会${aa}";
        JSONObject map = new JSONObject();
        map.put("aa","我");
        System.out.println(getMassage(aa,map));
}
    private String getMassage(String massageTemplet, JSONObject map) {
        String re = "";
        if (massageTemplet != null && map != null && !map.isEmpty()) {
            re = massageTemplet;
            Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Object> row = it.next();
                re = re.replace("${" + row.getKey() + "}", row.getValue() != null ? row.getValue().toString() : "");
            }
        }
        return re;
    }
}
